package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
	"time"
)

func main() {
	ddr := os.Getenv("USERPROFILE")
	fmt.Println("USERPROFILE为:", ddr)
	//上面的打印结果是：C:\Users\0921ssk
	
	//plgOut := "data.json"
	//if IsExist(plgOut) {
	//	fmt.Println("data.json is exist")
	//} else {
	//	fmt.Println("data.json is not exist")
	//}
	
	//copyOperate()
	//os.IsExist(err)
	//readMemoryLimit()
	
	readFileLineOperate()
	//readLineTest()
	now := time.Now()
	fmt.Println(time.Since(now))
}

func readLineTest() {
	file, err := os.Open("panic.go")
	if err != nil {
		fmt.Println(err)
	}
	reader := bufio.NewReader(file)
	for {
		string1, err := reader.ReadString('\n')
		defer file.Close()
		if err != nil {
			fmt.Println("错误err：为")
		}
		if err == io.EOF {
			fmt.Println("read over")
			break
		}
		fmt.Printf("read  is %s", string1)
	}
}

func readFileLineOperate() {
	//body2.txt
	//file, err2 := os.OpenFile("./panic.go", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	file, err2 := os.Open("body2.txt")
	if err2 != nil {
		log.Println("无法打开解析body2.txt")
		fmt.Println("无法打开解析body2.txt")
		return
	}
	readER := bufio.NewReader(file)
	outTemp := ""
	for {
		line, err := readER.ReadString(110)
		defer file.Close()
		if err != nil && len(line) == 0 {
			fmt.Println("len(line) = 0退出循环")
			break
		}
		fmt.Println("len(line) ", line)
		if strings.Contains(line, "key") {
			outTemp = line
			fmt.Println("tem", outTemp)
		}
		
	}
}

func readMemoryLimit() {
	//学习链接：https://blog.csdn.net/weixin_44178736/article/details/120616113
	url := "https://www.baidu.com/"
	resp, err := http.Get(url)
	if err != nil {
		fmt.Printf("Http.Get(url) err: %v.\n", err)
		return
	}
	
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("ioutil.ReadAll(resp.Body) err: %v.\n", err)
		return
	}
	resp.Body.Close()
	ioutil.WriteFile("body.txt", b, 0600)
}

func copyOperate() {
	url := "https://www.baidu.com/"
	resp, err := http.Get(url)
	if err != nil {
		fmt.Printf("Http.Get(url) err: %v.\n", err)
		return
	}
	otherF, err := os.Create("body2.txt")
	if err != nil {
		fmt.Printf("os.Create() err: %v.\n", err)
	}
	io.Copy(otherF, resp.Body)
	resp.Body.Close()
}

func IsExist(path string) bool {
	_, err := os.Stat(path)
	if err != nil {
		//log.Println("xutil.IsExist", err, "os.IsExist", os.IsExist(err))
	}
	return err == nil || os.IsExist(err)
}
